package com.sele.dataprovider;
import java.util.List;
import java.util.ArrayList;

import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;

import com.sele.utils.CustomUtils;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;  
import org.apache.poi.ss.usermodel.Row;  
import org.apache.poi.ss.usermodel.Sheet;  
 
 
 /**
  * 参考网址:
  * http://blog.csdn.net/alchaochao/article/details/44922287
  * Excel放在Data文件夹下</p>
  * Excel命名方式：测试类名.xls</p>
  * Excel的sheet命名方式：测试方法名</p>
  * Excel第一行为Map键值</p>
  * 
  * excel格式参考网址:https://testerhome.com/topics/6852
  * @ClassName: ExcelDataProvider
  * @Description: TODO(读取Excel数据)
  */

 public class ExcelDataProvider implements IDataProvider{
 
     private Workbook workbook         = null;
     private Sheet    sheet        = null;
     //行的长度
     private int      rowLength     = 0;
     private int      currentRowNo = 0;
     //列的长度
     private int      columnLength    = 0;
     private String[] columnName;
     private static final Logger logger = LoggerFactory.getLogger(ExcelDataProvider.class);
     /**
      * 
      * @param classname 文件名
      * @param methodname sheet名称
      */
     public ExcelDataProvider(String fileName, String sheetname) {
 
         try {
 
            
             //从/data文件夹下读取以类名命名的excel文件
             //InputStream inputStream = CustomUtils.getInputStreamFromFile(fileName);
             //InputStream inputStream = new FileInputStream(is);
 
             workbook = CustomUtils.getWorkBook(fileName);
             //取sheet
             sheet = workbook.getSheet(sheetname);
             rowLength = sheet.getLastRowNum();
             //获得当前sheet的开始行  
             int firstRowNum  = sheet.getFirstRowNum();
             currentRowNo = firstRowNum;
             //获得当前行的列数  
             int lastCellNum = sheet.getRow(firstRowNum).getPhysicalNumberOfCells();
             columnLength = lastCellNum;
             //创建数组
             columnName = new String[columnLength];
             //获取当前行
             Row row = sheet.getRow(currentRowNo);
             Cell cell;
             //获取第一行的列名
             for (int i = 0; i < columnLength; i++) {
            	 cell =row.getCell(i);
                 columnName[i] = CustomUtils.getCellValue(cell);  
             } 
             this.currentRowNo++; 
 
         } catch (Exception e) {
             e.printStackTrace();
             Assert.fail("unable to read Excel data");
         }
     }
 
    //判断Excel表中是否存在下一行数据
	public boolean hasNext() {
 
         if (this.rowLength == 0 || this.currentRowNo >= this.rowLength) {
             try {
                 workbook.close();
             } catch (Exception e) {
            	 logger.error(e.getMessage());
             }
             return false;
         } else {
             // sheet下一行内容为空判定结束
             if (StringUtils.isBlank(CustomUtils.getCellValue(sheet.getRow(currentRowNo).getCell(0)))){
                 return false;
             }
             return true;
         }
     }
	
	
	 //读取Excel中某一行数据，并为下一行做准备
     public List<String> next() {
    	 Row row = sheet.getRow(currentRowNo);
         List<String> list = new ArrayList<String>();
         for (int i = 0; i < columnLength; i++) {
             String temp = "";
             try {
                 temp = CustomUtils.getCellValue(row.getCell(i));
             } catch (ArrayIndexOutOfBoundsException ex) {
                 temp = "";
             }
             //if(temp != null&& !temp.equals(""))
             list.add(temp);
         }
         
         this.currentRowNo++;
         return list;
     }
     
    public void remove() {
         throw new UnsupportedOperationException("remove unsupported.");     
     }

	public String[] getColumnName() {
		return columnName;
	}
    
    
 }